package com.jeeplus.modules.common.excel.defaultComponent;

import com.easy.core.util.StringUtils;
import com.jeeplus.modules.api.importlog.service.ImportLogService;
import com.jeeplus.modules.common.constant.CommonConstants;
import com.jeeplus.modules.common.excel.component.operator.*;
import com.jeeplus.modules.common.utils.SpringBeanUtils;
import com.jeeplus.modules.da.utils.BaseRedisService;

import java.util.List;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className DefaultRead
 * @description 默认的组件
 * @date 2022/7/12 17:49
 */
public class DefaultExcelParameter<T> implements InterfaceOperator<T> {
    private BaseRedisService baseRedisService;
    private ImportLogService importLogService;
    public DefaultExcelParameter() {
        this.baseRedisService = SpringBeanUtils.getBean(BaseRedisService.class);
        this.importLogService = SpringBeanUtils.getBean(ImportLogService.class);
    }
    /**
     * @author wang.yishu
     * @description 导入数据之前的操作
     * @date 2022/7/12 9:30
     * @return 导入之前接口
     */
    public BeforeRead beforeRead() {
        return new BeforeRead() {
            @Override
            public void beforeRead(String key, Map<String, Integer> importProcessMap) {
                //缓存中如存在导入转专业的数据，不可继续操作
                if(StringUtils.isNotNull(baseRedisService.getString(key))){
                    throw new RuntimeException("您有导入任务的进程，请查看导入结果或稍后再试。");
                }
                baseRedisService.setObjectToRedis(key, importProcessMap);
            }
        };
    }
    /**
     * @author wang.yishu
     * @description 导入之中的操作
     * @date 2022/7/12 9:31
     * @return 导入之中的操作
     */
    public InRead inRead() {
        return new InRead() {
            @Override
            public void inRead(String key, Map<String, Integer> importProcessMap) {
                baseRedisService.setObjectToRedis(key, importProcessMap, 3L);
            }
        };
    }

    /**
     * @author wang.yishu
     * @description 导入之后的操作
     * @date 2022/7/12 9:32
     * @return 导入之后的操作
     */
    public AfterRead<T> afterRead() {
        return new AfterRead<T>() {
            @Override
            public void afterRead(List<T> errorList, int count, int success, int failed, String key, Class<T> tClass) {
                importLogService.saveErrLogExcelNew(errorList, count, success, failed, key,tClass);
            }
        };
    }

    /**
     * @author wang.yishu
     * @description 导入最终操作
     * @date 2022/7/12 10:02
     * @return 导入最终操作
     */
    public FinalRead finalRead() {
        return new FinalRead() {
            @Override
            public void finalRead(String key) {
                baseRedisService.deleteKey(key);
            }
        };
    }

}
